/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Core;

import Common.DateAndTime;
import Core.Rule.Period;
import java.util.*;
import Database.*;
import Interfaces.*;
import java.text.SimpleDateFormat;
/**
 *
 * @author harish
 */
public class SmartHome
{
    private int _smartHomeId=0;
    private List<Appliance> _appliances=new ArrayList<Appliance>();
    private int _policyId=0;
    private List<Rule> _rules=new ArrayList<Rule>();
    
    public SmartHome()
    {
        InsertSmartHome();
        RandomizeAllocationOfAppliances();       
    }
    
    private void InsertSmartHome()
    {
        Database.GetInstance().InsertHome(Configuration.GetInstance().GetCurrentConfiguration());
        _smartHomeId=Database.GetInstance().ReadCurrentHome();
    }
    
    private void RandomizeAllocationOfAppliances()
    {
       for(Appliance a: Configuration.GetInstance().GetEssentialAppliances() )
       {
           _appliances.add(a);
           Database.GetInstance().InsertAllocation(_smartHomeId, a._applianceId);
       }
       Random r=new Random();
       int ran=r.nextInt(Configuration.GetInstance().GetNoOfNonEssentialAppliances());
       for(int i=0;i<ran;i++)
       {
           int rand=r.nextInt(ran);
           Appliance app=Configuration.GetInstance().GetNonEssentialAppliances().get(rand);
           _appliances.add(app);
           Database.GetInstance().InsertAllocation(_smartHomeId, app._applianceId);
       }
    }
    
    public void InsertData()
    {
        for(Appliance a:_appliances)
        {
            Data data=new Data();
            data.applianceId=a._applianceId;
            data.homeId=_smartHomeId;
            data.policyId=_policyId;
            data.start_time=Processor.DateTime;
            data.end_time=SetEndTime(data.applianceId);
            Processor._data.add(data);          
        }
    }    
    
    public void SetPolicy(IPolicy p)
    {
        _rules=p.GetPolicy();
    }
    
    private Calendar SetEndTime(int applianceId)
    {
        Calendar result=Calendar.getInstance();
        int steps=0;
        for(Rule r:_rules)
        {
            if(r.appliance._applianceId==applianceId)
            {
                if(r.period==Period.daily)
                {
                    steps=24*60;
                }
                else if(r.period==Period.hourly)
                {
                    steps=60;
                }
                else if(r.period==Period.weekly)
                {
                    steps=24*60*7;
                }
                else 
                {
                    steps=24*60*7*30;
                }       
                if((Processor.DateTime.after(r.startTime)) && (Processor.DateTime.before(r.endTime)))             
                {
                    result=r.endTime;                   
                    
                }
                else
                {
                    result=r.startTime;
                }
                r.startTime.add(Calendar.MINUTE, steps);
                r.endTime.add(Calendar.MINUTE, steps);
            }
            
        }
        return result;
    }
}
